package com.hk.rabbitmq.three;

import com.hk.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;

import java.util.Scanner;

/**
 * @author: dpb
 * @date: 2022/3/14
 *
 * 消息在手动应答时是不丢失的，放回到队列中重新消费
 */
public class Task02 {
    public final static String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        //队列持久化
        boolean durable = true;
        //开启信道确认
        channel.confirmSelect();
        channel.queueDeclare(TASK_QUEUE_NAME, durable, false, false, null);
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String message = input.nextLine();
//            channel.basicPublish("", TASK_QUEUE_NAME, null, message.getBytes("UTF-8"));
            //MessageProperties.PERSISTENT_TEXT_PLAIN ----> 设置消息持久化
            channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"));
            System.out.println("生产者发出消息：" + message);
        }
    }
}
